package com.xtt.dynamic.datasource.plugin;

import com.xtt.dynamic.datasource.config.DynamicDataSource;
import com.xtt.dynamic.datasource.entity.Operation;
import org.apache.ibatis.executor.Executor;
import org.apache.ibatis.mapping.MappedStatement;
import org.apache.ibatis.mapping.SqlCommandType;
import org.apache.ibatis.plugin.*;
import org.apache.ibatis.session.ResultHandler;
import org.apache.ibatis.session.RowBounds;
import org.springframework.stereotype.Component;

//@Component
//@Intercepts({
//        @Signature(type = Executor.class, method = "update", args = {MappedStatement.class,Object.class}),
//        @Signature(type = Executor.class, method = "query", args = {MappedStatement.class, Object.class,RowBounds.class,ResultHandler.class})
//})
public class DynamicDataSourcePlugin implements Interceptor {

    @Override
    public Object intercept(Invocation invocation) throws Throwable {
        //拿到当前方法(update、query)所有参数
        Object[] objects = invocation.getArgs();
        //MappedStatement 封装CRUD所有的元素和SQL
        MappedStatement ms = (MappedStatement) objects[0];
        //读方法
        if (ms.getSqlCommandType().equals(SqlCommandType.SELECT)) {
            DynamicDataSource.name.set(Operation.READ);
        } else {
            //写方法
            DynamicDataSource.name.set(Operation.Write);
        }
        //修改当前线程要选择数据源的key
        return invocation.proceed();
    }

    @Override
    public Object plugin(Object target) {
        //判断是否拦截这个类对象(根据@intercepts注解决定)，然后决定是否返回一个代理对象还是返回原对象
        if (target instanceof Executor) {
            return Plugin.wrap(target, this);
        } else {
            return target;
        }
    }
}
